using System;
using System.Collections.Generic;
using UnityEngine;

public class UIStyle : ScriptableObject
{
	public delegate void OnStyleChanged();

	[Serializable]
	public class StyleClass
	{
		public string styleName;

		public List<StyleProperty> properties = new List<StyleProperty>();

		public void ApplyTo(UIWidget widget)
		{
			foreach (StyleProperty property in properties)
			{
				property.ApplyTo(widget);
			}
		}

		public StyleClass Clone()
		{
			StyleClass styleClass = new StyleClass();
			styleClass.styleName = styleName;
			for (int i = 0; i < properties.Count; i++)
			{
				StyleProperty styleProperty = properties[i];
				styleClass.properties.Add(styleProperty.Clone());
			}
			return styleClass;
		}
	}

	[Serializable]
	public class SpriteValue
	{
		public string atlasName;

		public string spriteName;

		public void ApplyTo(UIWidget widget)
		{
			UISprite uISprite = widget as UISprite;
			if (!(uISprite == null))
			{
				uISprite.atlas = instance.GetAtlas(atlasName);
				uISprite.spriteName = spriteName;
			}
		}
	}

	[Serializable]
	public class StyleProperty
	{
		public enum PropertyType
		{
			Color = 0,
			Sprite = 1,
			LabelEffect = 2,
			UseGradient = 3,
			Size = 4,
			LocalPosition = 5,
			Active = 6,
			Enable = 7,
			Pivot = 8,
			FontSlant = 9
		}

		public PropertyType type;

		public Color colorValue = Color.white;

		public Vector2 vector2Value;

		public Vector3 vector3Value;

		public int intValue;

		public float floatValue;

		public SpriteValue spriteValue;

		public bool boolValue;

		public UILabel.Effect effectTypeValue;

		public UIWidget.Pivot pivot;

		public StyleProperty Clone()
		{
			StyleProperty styleProperty = new StyleProperty();
			styleProperty.type = type;
			styleProperty.colorValue = colorValue;
			styleProperty.vector2Value = vector2Value;
			styleProperty.vector3Value = vector3Value;
			styleProperty.intValue = intValue;
			styleProperty.floatValue = floatValue;
			styleProperty.spriteValue = spriteValue;
			styleProperty.boolValue = boolValue;
			styleProperty.effectTypeValue = effectTypeValue;
			styleProperty.pivot = pivot;
			return styleProperty;
		}

		public void ApplyTo(UIWidget widget)
		{
			if (type == PropertyType.Color)
			{
				widget.color = colorValue;
			}
			else if (type == PropertyType.Sprite)
			{
				spriteValue.ApplyTo(widget);
			}
			else if (type == PropertyType.LabelEffect)
			{
				UILabel uILabel = widget as UILabel;
				if (!(uILabel == null))
				{
					uILabel.effectStyle = effectTypeValue;
					uILabel.effectDistance = vector2Value;
					uILabel.effectColor = colorValue;
				}
			}
			else if (type == PropertyType.UseGradient)
			{
				UILabel uILabel2 = widget as UILabel;
				if (!(uILabel2 == null))
				{
					uILabel2.applyGradient = boolValue;
				}
			}
			else if (type == PropertyType.LocalPosition)
			{
				widget.cachedTransform.localPosition = vector3Value;
			}
			else if (type == PropertyType.Size)
			{
				widget.width = (int)vector2Value.x;
				widget.height = (int)vector2Value.y;
			}
			else if (type == PropertyType.Active)
			{
				UITools.SetActive(widget, boolValue);
			}
			else if (type == PropertyType.Enable)
			{
				if (widget != null)
				{
					widget.enabled = boolValue;
				}
			}
			else if (type == PropertyType.Pivot)
			{
				widget.pivot = pivot;
			}
			else if (type == PropertyType.FontSlant)
			{
				UILabel uILabel3 = widget as UILabel;
				if (!(uILabel3 == null))
				{
				}
			}
		}
	}

	[Serializable]
	public class UIStyleId
	{
		public string styleName;

		public int index
		{
			get
			{
				return instance.GetStyleIndex(styleName);
			}
		}

		public StyleClass style
		{
			get
			{
				return instance.GetStyle(styleName);
			}
		}
	}

	[Serializable]
	public class NamedUIAtlas
	{
		public string name;

		public UIAtlas atlas;
	}

	protected static UIStyle instance_;

	protected static string styleName;

	public List<StyleClass> styles = new List<StyleClass>();

	public List<NamedUIAtlas> atlases = new List<NamedUIAtlas>();

	private Dictionary<string, StyleClass> stylesMap_;

	public static UIStyle instance
	{
		get
		{
			if (instance_ == null || (!Application.isPlaying && ConfigBase.instance.styleName != styleName))
			{
				styleName = ConfigBase.instance.styleName;
				instance_ = Resources.Load<UIStyle>(styleName);
				if (instance_ == null)
				{
					styleName = typeof(UIStyle).ToString();
					instance_ = Resources.Load<UIStyle>(styleName);
				}
			}
			return instance_;
		}
	}

	public Dictionary<string, StyleClass> stylesMap
	{
		get
		{
			if (stylesMap_ == null)
			{
				stylesMap_ = new Dictionary<string, StyleClass>();
				for (int i = 0; i < styles.Count; i++)
				{
					StyleClass styleClass = styles[i];
					if (!stylesMap_.ContainsKey(styleClass.styleName))
					{
						stylesMap_[styleClass.styleName] = styleClass;
					}
				}
			}
			return stylesMap_;
		}
	}

	public static event OnStyleChanged onStyleChanged;

	public static void ChangeStyle(string newStyleName)
	{
		styleName = newStyleName;
		instance_ = Resources.Load<UIStyle>(styleName);
		if (instance_ == null)
		{
			styleName = typeof(UIStyle).ToString();
			instance_ = Resources.Load<UIStyle>(styleName);
		}
		RaiseStyleChangedEvent();
	}

	public static void RaiseStyleChangedEvent()
	{
		if (UIStyle.onStyleChanged != null)
		{
			UIStyle.onStyleChanged();
		}
	}

	public StyleClass GetStyle(string styleName)
	{
		Dictionary<string, StyleClass> dictionary = stylesMap;
		if (dictionary != null)
		{
			if (styleName != null && dictionary.ContainsKey(styleName))
			{
				return dictionary[styleName];
			}
		}
		else
		{
			for (int i = 0; i < styles.Count; i++)
			{
				StyleClass styleClass = styles[i];
				if (styleClass.styleName == styleName)
				{
					return styleClass;
				}
			}
		}
		return null;
	}

	public int GetStyleIndex(string styleName)
	{
		int num = 0;
		foreach (StyleClass style in styles)
		{
			if (style.styleName == styleName)
			{
				return num;
			}
			num++;
		}
		return -1;
	}

	public UIAtlas GetAtlas(string atlasName)
	{
		for (int i = 0; i < atlases.Count; i++)
		{
			NamedUIAtlas namedUIAtlas = atlases[i];
			if (namedUIAtlas.name == atlasName)
			{
				return namedUIAtlas.atlas;
			}
		}
		return null;
	}

	public int GetAtlasIndex(string atlasName)
	{
		int num = 0;
		foreach (NamedUIAtlas atlase in atlases)
		{
			if (atlase.name == atlasName)
			{
				return num;
			}
			num++;
		}
		return -1;
	}
}
